﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Drawing;
using System.IO;
using System.Net;
using System.Text;
using System.Xml;

namespace DotNETX.Utility.Net
{
    public class XmlHttpRequest
    {

        public XmlHttpRequest()
        {
            Headers = new NameValueCollection();
        }

        public XmlDocument LoadXml(string baseUrl)
        {
            HttpWebRequest request;
            HttpWebResponse response = null;
            XmlDocument XMLdoc = new XmlDocument();
            try
            {
                request = CreateHttpWebRequest(baseUrl);
                response = (HttpWebResponse)request.GetResponse();
                XMLdoc.Load(response.GetResponseStream());
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                XMLdoc = null;
            }
            if (response != null) response.Close();
            return XMLdoc;
        }

        public Image LoadImage(string baseUrl)
        {
            HttpWebRequest request;
            HttpWebResponse response = null;
            Bitmap bitmap = null;
            try
            {
                request = CreateHttpWebRequest(baseUrl);
                response = (HttpWebResponse)request.GetResponse();
                bitmap = new Bitmap(response.GetResponseStream());
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            if (response != null) response.Close();
            return bitmap as Image;
        }

        public string LoadText(string baseUrl)
        {
            HttpWebRequest request;
            HttpWebResponse response = null;
            string s = null;
            try
            {
                request = CreateHttpWebRequest(baseUrl);
                response = (HttpWebResponse)request.GetResponse();
                StreamReader reader = new StreamReader(response.GetResponseStream());
                s = reader.ReadToEnd();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            if (response != null) response.Close();
            return s;
        }

        public byte[] LoadBytes(string baseUrl)
        {
            HttpWebRequest request;
            HttpWebResponse response = null;
            byte[] bytes = null;
            try
            {
                request = CreateHttpWebRequest(baseUrl);
                response = (HttpWebResponse)request.GetResponse();
                Stream s = response.GetResponseStream();

                bytes = StreamUtils.ReadFully(s);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            if (response != null) response.Close();
            return bytes;
        }

        private HttpWebRequest CreateHttpWebRequest(string baseUrl)
        {
            HttpWebRequest request;
            request = (HttpWebRequest)WebRequest.Create(string.Format(baseUrl));
            request.UserAgent = UserAgent;
            request.Headers.Add(Headers);
            return request;
        }

        public string UserAgent { get; set; }
        public NameValueCollection Headers { get; set; }
    }
}
